package com.yitiao.component;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.UUID;

@Component
public class RabbitSender {
    @Resource
    private RabbitTemplate rabbitTemplate;
    /**
     * 这里就是确认消息的回调监听接口，用于确认消息是否被broker所收到
     */
    final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback(){
        /**
         * 	@param correlationData 作为一个唯一的标识
         * 	@param ack broker 是否落盘成功
         * 	@param cause 失败的一些异常信息
         */
        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            System.err.println("消息ACK结果:" + ack + ", correlationData: " + correlationData.getReturnedMessage());
        }
    };

    /**
     * 	对外发送消息的方法
     * @param message 	具体的消息内容
     * @param properties   额外的附加属性,优先级、延迟时间
     * @throws Exception
     */
    public void send(Object message, Map<String, Object> properties) throws Exception {
        MessageHeaders headers = new MessageHeaders(properties);
        Message<?> objectMessage = MessageBuilder.createMessage(message, headers);

        rabbitTemplate.setConfirmCallback(confirmCallback);

//        channel.confirmSelect();
//        channel.txSelect();
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString().trim());

        rabbitTemplate.convertAndSend("exchange-1", "springboot.rabbit", objectMessage, new MessagePostProcessor() {
            @Override
            public org.springframework.amqp.core.Message postProcessMessage(org.springframework.amqp.core.Message message) throws AmqpException {
                System.out.println("------post to do");
                return message;
            }
        },correlationData);

    }
}
